package com.mrd.frame.onlinepay;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.domain.AlipayTradeCancelModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.request.AlipayTradeCancelRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.mrd.frame.service.AccessConfigService;

/**
 * 支付宝通用查询封装
 * @author han
 */
public class AliPayApi {
	@Autowired
	AccessConfigService accessConfigService;
	@Value("${pay.airpay.openApiDomain}")
	public String openApiDomain; // 支付宝openapi域名
	public static String trade_type = "NATIVE";//支付方式，扫码支付
	@Autowired
	protected QrCreateBean arCreateBean;
	
	/**
	 * 网关状态编码
	 * @author han
	 */
	public enum AliCodeEnu {
	    CECUSS("10000")    //成功
	    ,SERVERUNUSE("20000")  //服务不可用
	    ,GRANTAUTHORITYFAULT ("20001")  //授权权限不足
	    ,MISPARAM("40001")  //	缺少参数
	    ,ILLEGALPARAM("40002")  // 非法参数
	    ,DEALFAIL("40004")//业务处理失败
	    ,AUTHORITYFAULT("40006");//权限不足
	    private String value;

	    AliCodeEnu(String value) {
	        this.value = value;
	    }

	    public String val() {
	        return value;
	    }
	}
	
	/**
	 * 订单交易状态
	 * @author han
	 */
	public enum AliTradeStatus {
		WAITBUYERPAY("WAIT_BUYER_PAY")    //交易创建，等待买家付款
	    ,TRADECLOSED("TRADE_CLOSED")  //未付款交易超时关闭，或支付完成后全额退款
	    ,TRADESUCCESS ("TRADE_SUCCESS")  //交易支付成功
	    ,TRADEFINISHED("TRADE_FINISHED");  //交易结束，不可退款
	    private String value;

	    AliTradeStatus(String value) {
	        this.value = value;
	    }

	    public String val() {
	        return value;
	    }
	}
	
	/**
	 * 查询返回编码
	 * @author han
	 */
	public enum AliQueryEnu {
	    CECUSS("ACQ.TRADE_HAS_SUCCESS")    //成功
	    ,SYSERROR("ACQ.SYSTEM_ERROR")  //系统错误
	    ,GRANTAUTHORITYFAULT ("ACQ.INVALID_PARAMETER")  //参数无效
	    ,MISPARAM("ACQ.TRADE_NOT_EXIST");  //查询的交易不存在
	    private String value;

	    AliQueryEnu(String value) {
	        this.value = value;
	    }

	    public String val() {
	        return value;
	    }
	}

	
	/**
	 * 交易查询接口
	 * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.8H2JzG&docType=4&apiId=757
	 * @param bizContent
	 * @return
	 * @throws AlipayApiException
	 */
	public boolean isTradeQuery(AlipayTradeQueryModel model,String userKey) throws AlipayApiException{
		AlipayTradeQueryResponse response = tradeQuery(model,userKey);
		if(response.isSuccess()){
			return true;
		}
		return false;
	}
	
	public AlipayTradeQueryResponse tradeQuery(AlipayTradeQueryModel model,String userKey) throws AlipayApiException{
		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
		request.setBizModel(model);
		return new AliPayApiConfig(openApiDomain,userKey,accessConfigService).build().execute(request);
	}
	
	/**
	 * 查询对账单下载地址
	 * @param bizContent
	 * @return
	 * @throws AlipayApiException
	 */
	public String billDownloadurlQuery(AlipayDataDataserviceBillDownloadurlQueryModel model,String userKey) throws AlipayApiException{
		AlipayDataDataserviceBillDownloadurlQueryResponse response =  billDownloadurlQueryToResponse(model,userKey);
		return response.getBillDownloadUrl();
	}
	
	public AlipayDataDataserviceBillDownloadurlQueryResponse  billDownloadurlQueryToResponse (AlipayDataDataserviceBillDownloadurlQueryModel model,String userKey) throws AlipayApiException{
		AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
		request.setBizModel(model);
		return new AliPayApiConfig(openApiDomain,userKey,accessConfigService).build().execute(request);
	}
	
	/**
	 * 退款
	 * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.SAyEeI&docType=4&apiId=759
	 * @param content
	 * @return
	 * @throws AlipayApiException
	 */
	public String tradeRefund(AlipayTradeRefundModel model,String userKey) throws AlipayApiException{
		AlipayTradeRefundResponse response = tradeRefundToResponse(model,userKey);
		return response.getBody();
	}
	public AlipayTradeRefundResponse tradeRefundToResponse(AlipayTradeRefundModel model,String userKey) throws AlipayApiException{
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		request.setBizModel(model);
		return new AliPayApiConfig(openApiDomain,userKey,accessConfigService).build().execute(request);
	}
	
	/**
	 * 交易撤销接口
	 * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.XInh6e&docType=4&apiId=866
	 * @param bizContent
	 * @return
	 * @throws AlipayApiException
	 */
	public boolean isTradeCancel(AlipayTradeCancelModel model,String userKey) throws AlipayApiException{
		AlipayTradeCancelResponse response = tradeCancel(model,userKey);
		if(response.isSuccess()){
			return true;
		}
		return false;
	}
	
	public AlipayTradeCancelResponse tradeCancel(AlipayTradeCancelModel model,String userKey) throws AlipayApiException{
		AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
		request.setBizModel(model);
		AlipayTradeCancelResponse response = new AliPayApiConfig(openApiDomain,userKey,accessConfigService).build().execute(request);
		return response;
	}
	
	/**
	 * 扫码支付
	 * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.i0UVZn&treeId=193&articleId=105170&docType=1#s4
	 * @param notifyUrl 
	 * @return
	 * @throws AlipayApiException 
	 */
	public String tradePrecreatePay(AlipayTradePrecreateModel model, String notifyUrl,String userKey) throws AlipayApiException{
		AlipayTradePrecreateResponse response = tradePrecreatePayToResponse(model,notifyUrl, userKey);
		return response.getBody();
	}
	public AlipayTradePrecreateResponse tradePrecreatePayToResponse(AlipayTradePrecreateModel model, String notifyUrl,String userKey) throws AlipayApiException{
		AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
		request.setBizModel(model);
		request.setNotifyUrl(notifyUrl);
		return new AliPayApiConfig(openApiDomain,userKey,accessConfigService).build().execute(request);
	}
}
